import { useMemo } from "react";
// useDeleteAdminProduct.ts
import { useMutation, useQueryClient } from "react-query";
import { adminBaseUrl } from "../..";

export const useDeleteAdminProduct = () => {
  const queryClient = useQueryClient();

  const deleteProductMutation = useMutation(
    async (productId: number) => {
      const response = await fetch(`${adminBaseUrl}/${productId}`, {
        method: "DELETE",
      });
      if (!response.ok) {
        throw new Error("Network response was not ok");
      }
      return response.json();
    },
    {
      onSuccess: () => {
        queryClient.invalidateQueries("products"); // Обновляем данные после успешного удаления товара
      },
    }
  );

  return useMemo(() => deleteProductMutation, [deleteProductMutation]);
};
